using System;
using UnityEngine;
using UnityEngine.UI;

namespace BToolkit
{
    public class PanelActivation : MonoBehaviour
    {
        public Text textDeviceID;
        public InputField inputField;
        public BButton btnCancel, btnCopy, btnClear, btnPast, btnActive;

        private string deviceID;
        private static PanelActivation panelActivation;

        public static void Show(Transform parent)
        {
            if (!panelActivation)
            {
                PanelActivation prefab = Resources.Load<PanelActivation>("PanelActivation");
                panelActivation = Instantiate(prefab, parent, false);
            }
        }

        public static void Dispose()
        {
            if (panelActivation)
            {
                Destroy(panelActivation.gameObject);
            }
        }

        private void Start()
        {
            deviceID = SystemInfo.GetDeviceID();
            textDeviceID.text = $"设备码：{deviceID}";
            CanvasScaler scaler = GetComponentInParent<CanvasScaler>();
            transform.localScale = Vector3.one * scaler.referenceResolution.y / 1080f;

            btnCancel.onClick.AddListener(() =>
            {
                Application.Quit();
            });

            btnCopy.onClick.AddListener(() =>
            {
                CopyAndPaste.Copy(deviceID);
                btnCopy.GetComponentInChildren<Text>().text = "复制成功";
                BTimer.Invoke(1, () => { btnCopy.GetComponentInChildren<Text>().text = "【复制】"; });
            });

            btnClear.onClick.AddListener(() =>
            {
                inputField.text = "";
            });

            btnPast.onClick.AddListener(() =>
            {
                inputField.text = CopyAndPaste.GetPaste();
            });

            btnActive.onClick.AddListener(() =>
            {
                if (string.IsNullOrEmpty(inputField.text))
                {
                    DialogTip.Show("请输入激活码");
                    return;
                }
                LicenceChecker.Instance.Activation(inputField.text);
            });
        }
    }
}